package cn.bonoon.kernel.expression.resolver;

import java.lang.reflect.Method;

import cn.bonoon.kernel.expression.StandardExpressionParser;
import cn.bonoon.kernel.expression.results.FixedExpressionValue;

public class FieldExpressionResolver extends BaseExpressionResolver{

	FieldExpressionResolver() {
		super("FIELD");
	}

	@Override
	protected void _internalResolver(StandardExpressionParser seParser, String[] commands) throws Exception{
		Class<?> cls = Class.forName(commands[1]);
		Object value = cls.getField(commands[2]).get(null);
		if(commands.length > 3){
			String methodName = commands[3];
			if(cls.isEnum() && "name".equals(methodName)){
				value = ((Enum<?>)value).name();
			}else if(cls.isEnum() && "ordinal".equals(methodName)){
				value = ((Enum<?>)value).ordinal();
			}else{
				Class<?> valueClass = value.getClass();
				Method method = valueClass.getMethod(methodName);
				value = method.invoke(value);
			}
		}
		seParser.value(new FixedExpressionValue(seParser, value));
		super._internalResolver(seParser, commands);
	}

}
